package com.wanger.swa.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanger.swc.model.Result;
import com.wanger.swa.domain.myEnum.TemplateEnum;
import com.wanger.swa.dto.template.query.TemplateQuery;
import com.wanger.swa.dto.template.req.TemplateAddReq;
import com.wanger.swa.dto.template.req.TemplateUpdateContentReq;
import com.wanger.swa.dto.template.req.TemplateUpdatePidReq;
import com.wanger.swa.dto.template.req.TemplateUpdateReq;
import com.wanger.swa.dto.template.resp.TemplateResp;
import com.wanger.swa.dto.template.resp.TemplateTreeResp;
import com.wanger.swa.dto.template.resp.tips.systemTool.SysToolInfo;
import com.wanger.swa.dto.template.resp.tips.fileInfo.FileInfoResp;
import com.wanger.swa.service.ise.TemplateService;
import com.wanger.swc.util.json.MyJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author: wanger
 * @Date: 2024/9/15 12:52
 * @Description: 模板
 */
@RestController
@Api(tags = "模板")
@RequiredArgsConstructor
@RequestMapping("/template")
public class TemplateController {

    private final TemplateService templateService;

    @ApiOperation("添加模板")
    @PostMapping("/addTemplate")
    public Result<TemplateResp> addTemplate(@RequestBody TemplateAddReq addDTO) {
        return Result.ok(templateService.addTemplate(addDTO));
    }

    @ApiOperation("修改模板")
    @PutMapping("/updateTemplate")
    public Result<TemplateResp> updateTemplate(@RequestBody TemplateUpdateReq updateDTO) {
        return Result.ok(templateService.updateTemplate(updateDTO));
    }

    @ApiOperation("修改模板内容")
    @PutMapping("/updateTemplateContent")
    public Result<Boolean> updateTemplateContent(@RequestBody TemplateUpdateContentReq updateDTO) {
        return Result.ok(templateService.updateTemplateContent(updateDTO));
    }

    @ApiOperation("模板详情")
    @GetMapping("/getOneTemplate")
    public Result<TemplateResp> getOneTemplate(String id) {
        return Result.ok(templateService.getOneTemplate(id));
    }

    @ApiOperation("模板内容")
    @GetMapping("/getTemplateContentById")
    public Result<String> getTemplateContentById(String id) {
        return Result.ok(templateService.getTemplateContentById(id));
    }

    @ApiOperation("模板树")
    @GetMapping("/getTree")
    public Result<List<TemplateTreeResp>> getTree(String pid) {
        return Result.ok(templateService.getTree(pid));
    }

    @ApiOperation("分页查询模板")
    @GetMapping("/pageTemplate")
    public Result<IPage<TemplateResp>> pageTemplate(TemplateQuery query) {
        return Result.ok(templateService.pageTemplate(query));
    }

    @ApiOperation("模板列表查询")
    @GetMapping("/listTemplate")
    public Result<List<TemplateResp>> listTemplate(TemplateQuery query) {
        return Result.ok(templateService.listTemplate(query));
    }

    @ApiOperation("删除模板")
    @DeleteMapping("/deleteTemplate")
    public Result<Void> deleteTemplate(String id) {
        templateService.deleteTemplate(id);
        return Result.ok();
    }

    @ApiOperation("模板枚举")
    @GetMapping("/getEnumTemplate")
    public Result<Map<String, List<Map<String, String>>>> getEnumTemplate() {
        return Result.ok(TemplateEnum.getList());
    }

    @ApiOperation("提示->数据源相关提示")
    @GetMapping("/tipsDataInfo")
    @CrossOrigin
    public Result<Object> tipsDataInfo(String databaseId) {
        return Result.ok(MyJson.map().pu("dataContent", templateService.tipsDataInfo(databaseId)));
    }

    @ApiOperation("提示->文件相关")
    @GetMapping("/tipsFileInfo")
    public Result<FileInfoResp> tipsFileInfo(String templateId) {
        return Result.ok(templateService.tipsFileInfo(templateId));
    }

    @ApiOperation("提示->系统工具")
    @GetMapping("/tipsSystemTool")
    public Result<Collection<SysToolInfo>> tipsSystemTool() {
        return Result.ok(templateService.tipsSystemTool());
    }

    @ApiOperation("导出模板")
    @GetMapping("/exportTemplate")
    public void exportTemplate(String id, HttpServletResponse response) throws IOException {
        OutputStream out = response.getOutputStream();
        response.reset();
        response.setContentType("application/octet-stream;charset=UTF-8");
        String filename = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN) + "_sw.zip";
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
        templateService.exportTemplate(id, out);
    }

    @ApiOperation("导入模板")
    @PostMapping("/importTemplate/{id}")
    public Result<Boolean> importTemplate(@PathVariable String id,
                                          @RequestParam("file") MultipartFile file) {
        return Result.ok(templateService.importTemplate(id, file));
    }

    @ApiOperation("修改上级id")
    @PostMapping("/updatePid")
    public Result<Boolean> updatePid(@RequestBody TemplateUpdatePidReq req) {
        return Result.ok(templateService.updatePid(req));
    }
}
